package com.andy.app_core;

import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Interceptor;

/**
 * @version V1.0
 * @描述:
 * @Company:
 * @auth: Created by AN .
 * @time: 2020/3/26 02:18
 * @des
 */
public class Configurator {
    private static final HashMap<Object,Object> CONFIG=new HashMap<Object ,Object>();
    private static final ArrayList<Interceptor> INTERCEPTOR=new ArrayList<>();

    // 静态内部类单例
    private static class Holder{
        private static final Configurator INSTANCE=new Configurator();
    }
    public static Configurator getInstance(){
        return Holder.INSTANCE;
    }
    private Configurator() {
        CONFIG.put(ConfigKeys.CONFIG_READY.name(),false);
    }
    final HashMap<Object,Object> getConfig(){
        return CONFIG;
    }
    final <T> T getConfiguration(Object key){
        checkConfigurator();
        final Object value = CONFIG.get(key);
        if(value ==null){
            throw new NullPointerException(key.toString()+"IS NULL");
        }
        return (T) value;
    }
    //所有的配置均返回自己,方便链式调用
    //配置APIHOST
    public final Configurator withApiHost(String host){
        CONFIG.put(ConfigKeys.API_HOST,host);
        return this;
    }
    //设置拦截器
    public final Configurator withInterceptors(ArrayList<Interceptor> interceptors){
        CONFIG.put(ConfigKeys.INTERCEPTOR,interceptors);
        return this;
    }

    //配置完成
    public void configure(){
        CONFIG.put(ConfigKeys.CONFIG_READY.name(),true );
    }
    //检查配置是否完成
    private void checkConfigurator(){
        boolean isReady= (boolean) CONFIG.get(ConfigKeys.CONFIG_READY.name());
        if(!isReady){
            throw new RuntimeException("Configurator is not ready! call configure()");
        }
    }
}
